

//#define _GNU_SOURCE


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <windows.h>
#ifndef M_PI
#    define M_PI 3.14159265358979323846
#endif
#include "edflib.h"




/* if you want to create an EDFplus file instead of BDFplus, outcomment the next line: */
//#define BDF_FORMAT



void remove_trailing_zeros(char *);

const int smp_freq=1;
double *buf;
  
int main(int argc, char *argv[])
{
  int i, j,
	  hdl,      
	  chns=1,      
	  fileduration=6,      
	  linear=1;
  double 
	freq=1,
	q;
  long long sampleswritten;
  char str[256];


buf=(double *)malloc(smp_freq*sizeof(double));
 hdl = edfopen_file_writeonly("freq_sweep.edf", EDFLIB_FILETYPE_EDFPLUS, chns);

  if(hdl<0)
  {
    printf("error: edfopen_file_writeonly()\n");

    return(1);
  }

  for(i=0; i<chns; i++)
  {
    if(edf_set_samplefrequency(hdl, i, smp_freq))
    {
      printf("error: edf_set_samplefrequency()\n");

      return(1);
    }
  }
  
  for(i=0; i<chns; i++)
  {
    if(edf_set_datarecord_duration(hdl, 100000)) // min duration is .025 seconds (= 2500* 10 uS.) (10uS is the libray's step size)
    {
      printf("error: edf_set_samplefrequency()\n");

      return(1);
    }
  }

  for(i=0; i<chns; i++)
  {
    if(edf_set_digital_maximum(hdl, i, 32767))
    {
      printf("error: edf_set_digital_maximum()\n");

      return(1);
    }
  }

  for(i=0; i<chns; i++)
  {
    if(edf_set_digital_minimum(hdl, i, -32768))
    {
      printf("error: edf_set_digital_minimum()\n");

      return(1);
    }
  }

  for(i=0; i<chns; i++)
  {
    if(edf_set_physical_maximum(hdl, i, 400.0))
    {
      printf("error: edf_set_physical_maximum()\n");

      return(1);
    }
  }

  for(i=0; i<chns; i++)
  {
    if(edf_set_physical_minimum(hdl, i, -400.0))
    {
      printf("error: edf_set_physical_minimum()\n");

      return(1);
    }
  }

  for(i=0; i<chns; i++)
  {
    if(edf_set_physical_dimension(hdl, i, "uV"))
    {
      printf("error: edf_set_physical_dimension()\n");

      return(1);
    }
  }

  if(edf_set_label(hdl, 0, "sweep"))
  {
    printf("error: edf_set_label()\n");

    return(1);
  }

  if(edf_set_equipment(hdl, "Software generated file"))
  {
    printf("edf_set_equipment()\n");

    return(1);
  }

  
  remove_trailing_zeros(str);
  edf_set_patientname(hdl, str);
  edfwrite_annotation_latin1(hdl, 0LL, -1LL, "Recording starts");
  sampleswritten = 0;
for(j=0; j<100; j++) //record duration=.025*count _max seconds
  {
	/*for(i=0; i<smp_freq; i++)
    {*/
	  //q=200*sin(2*(3.14159)*1*(sampleswritten*(.025/smp_freq)));
		q=200*sin(2*(3.14159)*.1*j);
		buf[0]=q;
    //}

    if(edfwrite_physical_samples(hdl, buf))
    {
      printf("error: edfwrite_physical_samples()\n");

      return(1);
    }
	////////////////////////////////////////
	Sleep(100);
	///////////////////////////////////////
    if(!(j%10))
    {
      sprintf(str, "%fHz", freq);
      remove_trailing_zeros(str);
      edfwrite_annotation_latin1(hdl, j * 10000LL, -1LL, str);
    }

  }

  sprintf(str, "%fHz", freq);
  remove_trailing_zeros(str);
  edfwrite_annotation_latin1(hdl, j * 10000LL, -1LL, str);

  edfwrite_annotation_latin1(hdl, fileduration * 10000LL, -1LL, "Recording ends");

  edfclose_file(hdl);

  return(0);
}



void remove_trailing_zeros(char *str)
{
  int i, j,
      len,
      numberfound,
      dotfound,
      decimalzerofound,
      trailingzerofound=1;

  while(trailingzerofound)
  {
    numberfound = 0;
    dotfound = 0;
    decimalzerofound = 0;
    trailingzerofound = 0;

    len = strlen(str);

    for(i=0; i<len; i++)
    {
      if((str[i] < '0') || (str[i] > '9'))
      {
        if(decimalzerofound)
        {
          if(str[i-decimalzerofound-1] == '.')
          {
            decimalzerofound++;
          }

          for(j=i; j<(len+1); j++)
          {
            str[j-decimalzerofound] = str[j];
          }

          trailingzerofound = 1;

          break;
        }

        if(str[i] != '.')
        {
          numberfound = 0;
          dotfound = 0;
          decimalzerofound = 0;
        }
      }
      else
      {
        numberfound = 1;

        if(str[i] > '0')
        {
          decimalzerofound = 0;
        }
      }

      if((str[i] == '.') && numberfound)
      {
        dotfound = 1;
      }

      if((str[i] == '0') && dotfound)
      {
        decimalzerofound++;
      }
    }
  }

  if(decimalzerofound)
  {
    if(str[i-decimalzerofound-1] == '.')
    {
      decimalzerofound++;
    }

    for(j=i; j<(len+1); j++)
    {
      str[j-decimalzerofound] = str[j];
    }
  }
}

















